/*
 *  Class name: SoccerMatchArrangeableTableHandler.java
 *  Version: 1.0
 *  Date: 22.3.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.managedbeans.entity.handlers.table;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.SoccerMatchDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.SoccerMatchServiceBeanLocal;
import cz.muni.fi.pa165.soccergamesarchive.datamodels.entity.SoccerMatchDataModel;
import cz.muni.fi.pa165.soccergamesarchive.structures.FilterValue;
import cz.muni.fi.pa165.soccergamesarchive.structures.RestrictionOperator;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.inject.Named;

import static java.util.Collections.emptyList;

/**
 *
 * @author Michal Ďuriš
 */
@Named(value = "soccerMatchArrangeableTableHandler")
@Dependent
public class SoccerMatchArrangeableTableHandler extends ArrangeableEntityTableHandler
        implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final int MAX_SUBTABLES_COUNT = 10;

    private final Map<Date, Boolean> subtablesExpandedStateMap = new HashMap<>();

    @EJB
    private SoccerMatchServiceBeanLocal matchServiceBean;

    private SoccerMatchDataModel matchDataModel;

    /**
     * Creates a new instance of SoccerMatchArrangeableTableHandler
     */
    public SoccerMatchArrangeableTableHandler() {
    }

    @PostConstruct
    public void init() {
        matchDataModel = new SoccerMatchDataModel(matchServiceBean);
        matchDataModel.setFilterValuesMap(filterValues);
    }

    public int getRowCount() {
        return MAX_SUBTABLES_COUNT;
    }

    @Override
    public Object getDataModel() {
        return matchDataModel;
    }

    public List<SoccerMatchDTO> getSoccerMatchesByDate(
            Date matchDay) {

        if (matchDay == null) {

            //TODO: LOGGER
            System.err.println(getClass().getName() + ": Invocation of method "
                               + "with illegal argument !");

            return emptyList();
        }

        List<SoccerMatchDTO> matchList;

        if (subtablesExpandedStateMap.containsKey(matchDay)
            && subtablesExpandedStateMap.get(matchDay)) {

            Map<String, Object> filterMap = new HashMap<>();

            if (filterValues != null || !filterValues.isEmpty()) {

                for (Map.Entry<String, String> entry : filterValues.entrySet()) {

                    filterMap.put(entry.getKey(), new FilterValue(entry
                            .getValue(), RestrictionOperator.AND));
                }
            }

            filterMap.put("matchDate", new FilterValue(matchDay,
                    RestrictionOperator.AND));

            matchList = matchServiceBean.getLimitedNumberOfMatches(
                    0, matchServiceBean.getFilteredSoccerMatchesCount(filterMap),
                    filterMap, null);
        } else {

            matchList = emptyList();
        }

        return matchList;
    }

    public void setSubtablesExpandedState(Date subtableKey) {

        if (subtablesExpandedStateMap.containsKey(subtableKey)) {

            if (subtablesExpandedStateMap.get(subtableKey)) {

                subtablesExpandedStateMap.put(subtableKey, Boolean.FALSE);
            } else {

                subtablesExpandedStateMap.put(subtableKey, Boolean.TRUE);
            }

        } else {
            subtablesExpandedStateMap.put(subtableKey, Boolean.TRUE);
        }
    }

    public boolean getSubtablesExpandedState(Date subtableKey) {

        if (subtablesExpandedStateMap.containsKey(subtableKey)) {

            return subtablesExpandedStateMap.get(subtableKey);
        } else {

            subtablesExpandedStateMap.put(subtableKey, Boolean.FALSE);
        }

        return false;
    }

}
